package com.beyondsoft.sabg.edu.controller;

import com.beyondsoft.sabg.edu.model.dto.DocumentOperationRequestDto;
import com.beyondsoft.sabg.edu.model.vo.AiMiaoBiAuditResultVO;
import com.beyondsoft.sabg.edu.model.vo.core.SimpleResponseVO;
import com.beyondsoft.sabg.edu.service.AiMiaoBiService;
import com.beyondsoft.sabg.edu.utils.ResponseUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

/**
 * 阿里云妙笔控制器
 */
@RestController
@RequestMapping("/api/aimiaobi")
@Slf4j
public class AiMiaoBiController {

    @Autowired
    private AiMiaoBiService aiMiaoBiService;

    /**
     * 智能写作 - 流式返回
     */
    @PostMapping("/writing/stream")
    public SseEmitter runWritingStream(@RequestBody DocumentOperationRequestDto request) {
        try {
            log.info("收到智能写作流式请求，文档类型：法定公文类");
            
            return aiMiaoBiService.runWritingStream(request);
        } catch (Exception e) {
            log.error("智能写作流式返回失败", e);
            SseEmitter emitter = new SseEmitter();
            emitter.completeWithError(e);
            return emitter;
        }
    }

    /**
     * 续写内容 - 流式返回
     */
    @PostMapping("/continue/stream")
    public SseEmitter runContinueContentStream(@RequestBody DocumentOperationRequestDto request) {
        try {
            log.info("收到续写流式请求，内容长度：{}", request.getSelectedText() != null ? request.getSelectedText().length() : 0);
            
            return aiMiaoBiService.runContinueContentStream(request);
        } catch (Exception e) {
            log.error("续写内容流式返回失败", e);
            SseEmitter emitter = new SseEmitter();
            emitter.completeWithError(e);
            return emitter;
        }
    }

    /**
     * 文本润色 - 流式返回
     */
    @PostMapping("/polish/stream")
    public SseEmitter runTextPolishingStream(@RequestBody DocumentOperationRequestDto request) {
        try {
            log.info("收到润色流式请求，内容长度：{}", request.getSelectedText() != null ? request.getSelectedText().length() : 0);
            
            return aiMiaoBiService.runTextPolishingStream(request);
        } catch (Exception e) {
            log.error("文本润色流式返回失败", e);
            SseEmitter emitter = new SseEmitter();
            emitter.completeWithError(e);
            return emitter;
        }
    }

    /**
     * 提交智能审查任务
     */
    @PostMapping("/audit/submit")
    public SimpleResponseVO submitSmartAudit(@RequestBody DocumentOperationRequestDto request) {
        try {
            log.info("收到审查任务提交请求，文本长度：{}", request.getFullDocumentContent() != null ? request.getFullDocumentContent().length() : 0);
            
            String taskId = aiMiaoBiService.submitSmartAudit(request);
            
            return ResponseUtil.success().withData(taskId);
        } catch (Exception e) {
            log.error("提交智能审查任务失败", e);
            return ResponseUtil.failed("提交智能审查任务失败：" + e.getMessage());
        }
    }

    /**
     * 获取智能审查结果
     */
    @GetMapping("/audit/result/{taskId}")
    public SimpleResponseVO getSmartAuditResult(@PathVariable String taskId) {
        try {
            log.info("收到获取审查结果请求，任务ID：{}", taskId);
            
            AiMiaoBiAuditResultVO result = aiMiaoBiService.getSmartAuditResult(taskId);
            
            return ResponseUtil.success().withData(result);
        } catch (Exception e) {
            log.error("获取智能审查结果失败", e);
            return ResponseUtil.failed("获取智能审查结果失败：" + e.getMessage());
        }
    }

    /**
     * 智能审查（同步方法）
     */
    @PostMapping("/audit")
    public SimpleResponseVO smartAudit(@RequestBody DocumentOperationRequestDto request) {
        try {
            log.info("收到同步审查请求，文本长度：{}", request.getFullDocumentContent() != null ? request.getFullDocumentContent().length() : 0);
            
            AiMiaoBiAuditResultVO result = aiMiaoBiService.smartAudit(request);
            
            return ResponseUtil.success().withData(result);
        } catch (Exception e) {
            log.error("智能审查失败", e);
            return ResponseUtil.failed("智能审查失败：" + e.getMessage());
        }
    }
} 